package flaud

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/flaud"
	flaudReq "github.com/flipped-aurora/gin-vue-admin/server/model/flaud/request"
)

type CaseFactService struct{}

// CreateCaseFact 创建查明案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (ICFService *CaseFactService) CreateCaseFact(ctx context.Context, ICF *flaud.CaseFact) (err error) {
	err = global.GVA_DB.Create(ICF).Error
	return err
}

// DeleteCaseFact 删除查明案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (ICFService *CaseFactService) DeleteCaseFact(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&flaud.CaseFact{}, "id = ?", ID).Error
	return err
}

// DeleteCaseFactByIds 批量删除查明案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (ICFService *CaseFactService) DeleteCaseFactByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]flaud.CaseFact{}, "id in ?", IDs).Error
	return err
}

// UpdateCaseFact 更新查明案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (ICFService *CaseFactService) UpdateCaseFact(ctx context.Context, ICF flaud.CaseFact) (err error) {
	err = global.GVA_DB.Model(&flaud.CaseFact{}).Where("id = ?", ICF.ID).Updates(&ICF).Error
	return err
}

// GetCaseFact 根据ID获取查明案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (ICFService *CaseFactService) GetCaseFact(ctx context.Context, ID string) (ICF flaud.CaseFact, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&ICF).Error
	return
}

// GetCaseFactInfoList 分页获取查明案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (ICFService *CaseFactService) GetCaseFactInfoList(ctx context.Context, info flaudReq.CaseFactSearch) (list []flaud.CaseFact, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&flaud.CaseFact{})
	var ICFs []flaud.CaseFact
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["defendant_name"] = true
	orderMap["crime_period"] = true
	orderMap["criminal_acts"] = true
	orderMap["card_details"] = true
	orderMap["illicit_profit"] = true
	orderMap["fraud_amount"] = true
	orderMap["card_used_for_fraud"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&ICFs).Error
	return ICFs, total, err
}
func (ICFService *CaseFactService) GetCaseFactPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
